package com.gao.h_manul_ack;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author gao
 * @time 2022/07/05 15:20:55
 */
//@Component
@Slf4j
public class Consumer {

    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            value = @Queue(name = "queue_test"),
                            exchange = @Exchange(value = "exchange_demo", type = ExchangeTypes.DIRECT),
                            key = "rk"
                    )
            }
    )
    public void receive(Message message, Channel channel) throws IOException {
        log.info("消费到消息：{}", new String(message.getBody()));
        // 手动确认消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
